<?php
/**
 * Created by PhpStorm.
 * User: yangsl
 * Date: 2016/6/21
 * Time: 13:11
 */

namespace common\components;

use yii\helpers\Html;

class Tools
{
    /**
     * 无极分类
     * @param $arr  传入数组
     * @param int $level 层级
     * @param string $sort 排序字段
     * @param boolean $prefix 每项前缀
     * @param integer $mode 前缀模式
     */
    public static function menuMake($arr, $level = -1, $sort = 'Sort', $menu_arr = array(), $prefix=true, $mode=1)
    {
        $nbsp = '&nbsp;&nbsp;&nbsp;&nbsp;';
        if(empty($arr)) return $arr;

        if($level < 0){
            //初始化
            foreach ($arr as $v) {
                $d = $v->attributes;
                $_data[] = $d;
                $_sort[] = $d[$sort];
            }
            //排序
            array_multisort($_sort, SORT_DESC, $_data);
            //分级
            foreach($_data as $v) {
                $_menu[$v['ParentId']][] = $v;
            }
            //开始分级
            foreach($_menu[0] as $v){
                $menu[] = $v;
                //判断是否有子分类。有的话递归
                if(isset($_menu[$v['Id']])){
                    $menu = array_merge($menu,self::menuMake($_menu[$v['Id']], 1, $sort,$_menu, $prefix, $mode));
                }
            }
//            var_dump($menu);
//            die;
            return $menu;
        }else{
            foreach($arr as $k=>$m){
                switch ($mode) {
                    case 1:
                        $m['CateName'] = $prefix ? Html::decode(str_repeat("│&nbsp;&nbsp;", $level)).($k+1<count($arr)?'├─&nbsp;':'└─&nbsp;').$m['CateName'] : $m['CateName'];
                        break;

                    case 2:
                        $m['CateName'] = $prefix ? Html::decode(str_repeat("&nbsp;&nbsp;&nbsp;", $level)).'└─&nbsp;'.$m['CateName'] : $m['CateName'];
                        break;
                }
                $ret[] = $m;
                if(isset($menu_arr[$m['Id']])){
                    $ret = array_merge($ret,self::menuMake($menu_arr[$m['Id']], $level+1, $sort,$menu_arr,$prefix, $mode));
                }
            }
            return $ret;
        }
    }


    public static function sendMsg($mobile,$tplId,$params)
    {
        header('content-type:text/html;charset=utf-8');

        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
        $paramStr = "";
        foreach ($params as $k=>$v) {
            $paramStr .= $paramStr == '' ? '#'.$k.'#='.$v : '&#'.$k.'#='.$v;
        }
        $smsConf = array(
            'key'   => '0a77607bf84e4310ac546e9476a92691', //您申请的APPKEY
            'mobile'    => $mobile, //接受短信的用户手机号码
            'tpl_id'    => $tplId, //您申请的短信模板ID，根据实际情况修改
            'tpl_value' =>$paramStr //您设置的模板变量，根据实际情况修改
        );
        //$smsConf['tpl_id'] = '16232';
        //$smsConf['mobile'] = '13196761198';
        $content = self::juhecurl($sendUrl,$smsConf,1); //请求发送短信

        // $resMsg = '';
        $isSuccess= false;
        if($content){
            $result = json_decode($content,true);
            $error_code = $result['error_code'];
            if($error_code == 0){
                //状态为0，说明短信发送成功
                $isSuccess = true;
         //       $resMsg = "短信发送成功,短信ID：".$result['result']['sid'];
            }else{
                //状态非0，说明失败
                $msg = $result['reason'];
           //     $resMsg = "短信发送失败(".$error_code.")：".$msg;
            }
        }else{
            //返回内容异常，以下可根据业务逻辑自行修改
            //$resMsg = "请求发送短信失败";
        }
        return $isSuccess;
    }
    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    function juhecurl($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();

        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }

}